﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Loadscreen
{
    public partial class Loadscreen : Form
    {
        MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
        
        int userID;
        int projectAmount;

        //hier komen title en description voor de projecten in
        string title;
        string description;
        string title2;
        string description2;
        string title3;
        string description3;
        string title4;
        string description4;
        string title5;
        string description5;
        int id1;
        int id2;
        int id3;
        int id4;
        int id5;
        //hier komen de project ID's in
        int[] projects = new int[10];
        MySqlDataReader reader = null;

        public Loadscreen()
        {

        }

        public Loadscreen(string name, string pass)
        {
            InitializeComponent();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Step = 18;
            string username = name;
            string password = pass;
       
            try
            {
                grabConnection.Open();
                //userID ophalen
                MySqlCommand getID = new MySqlCommand("SELECT UserID FROM confirmed_users WHERE Email='"+username+"'", grabConnection);

                reader = getID.ExecuteReader();

                //userID ophalen
                while (reader.Read())
                {
                    userID = Convert.ToInt32(reader["userID"].ToString());
                }

                grabConnection.Close();

                grabConnection.Open();

                MySqlCommand getProjectAmount = new MySqlCommand("SELECT COUNT(UserID) AS AMOUNT FROM project_members WHERE UserID='" + userID + "'", grabConnection);
                reader = getProjectAmount.ExecuteReader();

                //aantal projecten ophalen
                while (reader.Read())
                {
                    projectAmount = Convert.ToInt32(reader["AMOUNT"].ToString());
                }

                grabConnection.Close();

                grabConnection.Open();

                MySqlCommand getProjectID = new MySqlCommand("SELECT ProjectID FROM project_members WHERE UserID='" + userID + "' ORDER BY ProjectID", grabConnection);
                reader = getProjectID.ExecuteReader();

                //projectIDID ophalen
                int counter = 0;
                while (reader.Read())
                {
                    projects[counter] = Convert.ToInt32(reader["ProjectID"].ToString());
                    counter++;
                }

                grabConnection.Close();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
                return;
            }
            ///////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 1)
            {
                try
                {
                    //projecttitel en beschrijving ophalen
                    grabConnection.Open();
                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[0] + "'", grabConnection);
                    reader = getProjectInfo.ExecuteReader();

                    while (reader.Read())
                    {
                        string id = reader["ProjectID"].ToString();
                        id1 = Int32.Parse(id);
                        title = reader["Projectnaam"].ToString();
                        description = reader["Projectbeschrijving"].ToString();
                    }
                    reader.Dispose();
                }
                catch (MySqlException e)
                {
                    MessageBox.Show("Sql error detected! @ project 1"+e.ToString());
              
                }
            }
            //////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 2)
            {
                try
                {
                    //projecttitel en beschrijving ophalen
                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[1] + "'", grabConnection);
                    MySqlDataReader reader2 = getProjectInfo.ExecuteReader();

                    while (reader2.Read())
                    {
                        string id = reader2["ProjectID"].ToString();
                        id2 = Int32.Parse(id);
                        title2 = reader2["Projectnaam"].ToString();
                        description2 = reader2["Projectbeschrijving"].ToString();
                    }
                    reader2.Dispose();
                }
                catch (MySqlException e)
                {
                    MessageBox.Show("Sql error detected! @ project 2" + e.ToString());
                }
            }
            ///////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 3)
            {
                try
                {
                    //projecttitel en beschrijving ophalen
                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[2] + "'", grabConnection);
                    MySqlDataReader reader3 = getProjectInfo.ExecuteReader();

                    while (reader3.Read())
                    {
                        string id = reader3["ProjectID"].ToString();
                        id3 = Int32.Parse(id);
                        title3 = reader3["Projectnaam"].ToString();
                        description3 = reader3["Projectbeschrijving"].ToString();
                    }
                    reader3.Dispose();
                }
                catch (MySqlException e)
                {
                    MessageBox.Show("Sql error detected! @ project 3" + e.ToString());
                }
            }
            ///////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 4)
            {
                try
                {
                    //projecttitel en beschrijving ophalen
                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[3] + "'", grabConnection);
                    MySqlDataReader reader4 = getProjectInfo.ExecuteReader();

                    while (reader4.Read())
                    {
                        string id = reader4["ProjectID"].ToString();
                        id4 = Int32.Parse(id);
                        title4 = reader4["Projectnaam"].ToString();
                        description4 = reader4["Projectbeschrijving"].ToString();
                    }
                    reader4.Dispose();
                }
                catch (MySqlException e)
                {
                    MessageBox.Show("Sql error detected! @ project 4" + e.ToString());
                }
            }
            ///////////////////////////////////////////////////////////////////////////////////////////

            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount == 5)
            {
                try
                {
                    //projecttitel en beschrijving ophalen
                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[4] + "'", grabConnection);
                    MySqlDataReader reader5 = getProjectInfo.ExecuteReader();

                    while (reader5.Read())
                    {
                        string id = reader5["ProjectID"].ToString();
                        id5 = Int32.Parse(id);
                        title5 = reader5["Projectnaam"].ToString();
                        description5 = reader5["Projectbeschrijving"].ToString();
                    }
                    reader5.Dispose();
                    grabConnection.Close();
                }
                catch (MySqlException e)
                {
                    MessageBox.Show("Sql error detected! @ project 5" + e.ToString());
                }
            }

            progressBar1.PerformStep();
            
            if(progressBar1.Value == 100)
            {
                Projectkeuzemenu.Projectkeuzemenu loadingDone = new Projectkeuzemenu.Projectkeuzemenu(userID, name, pass, projectAmount, title, description, title2, description2, title3, description3, title4, description4, title5, description5, id1, id2, id3, id4, id5);
                loadingDone.Show();
            }
        }
    }
}
